/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scenedemo.animatiom.sipnner;

import com.bytedance.scenedemo.ResourceTable;

import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;

import java.util.List;

/**
 * AttachListPopupView.
 *
 * @since 2021-06-17
 */
public class AttachListPopupView extends AttachPopupView {
    private static final int STATUS_BAR_HEIGHT = 130;
    private List<String> mData;
    private OnSelectListener mSelectListener;

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param popupInfo popupInfo
     */
    public AttachListPopupView(Context context, PopupInfo popupInfo) {
        super(context, popupInfo);
        addInnerContent();
    }

    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_popup_attach_impl_list;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        final EasyProvider<String> adapter = new EasyProvider<String>(getContext(), mData,
                ResourceTable.Layout_popup_adapter_text) {
            @Override
            protected void bind(ViewHolder holder, String itemData, int position) {
                holder.setText(ResourceTable.Id_tv_text, itemData);
                holder.<Text>getView(ResourceTable.Id_tv_text).setTextColor(new Color(PopupUtils.getColor(getContext(),
                        ResourceTable.Color_popup_text_dark_color)));
            }
        };
        listContainer.setItemClickedListener((listContainer1, component, position, id) -> {
            if (mSelectListener != null) {
                mSelectListener.onSelect(position, adapter.getData().get(position));
            }
            if (getPopupInfo().getAutoDismiss()) {
                dismiss();
            }
        });
        listContainer.setItemProvider(adapter);
        applyTheme();
    }

    /**
     * 主题
     */
    protected void applyTheme() {
        applyLightTheme();
    }

    @Override
    protected void applyLightTheme() {
        super.applyLightTheme();
    }

    @Override
    protected int setStatusBarHeight() {
        return STATUS_BAR_HEIGHT;
    }

    /**
     * 设置数据源
     *
     * @param data 数据源
     * @return AttachListPopupView
     */
    public AttachListPopupView setStringData(List<String> data) {
        this.mData = data;
        return this;
    }

    /**
     * 设置选择的监听
     *
     * @param selectListener OnSelectListener
     * @return AttachListPopupView
     */
    public AttachListPopupView setOnSelectListener(OnSelectListener selectListener) {
        this.mSelectListener = selectListener;
        return this;
    }
}
